home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / appgen-0.2-a / appgen-0 / AppGEN / src / compiler / appgen.c next >
Encoding:
C/C++ Source or Header  |  1996-07-11  |  48.1 KB  |  1,520 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4.  
  5. #define MAX_MENUS 32
  6. #define MAX_FORMS 2
  7. #define MAX_PATH 256
  8. #define WORD_SIZE 800 
  9. #define LINE_SIZE 800
  10. #define INT 1
  11. #define STRING 2
  12. #define DATA 3
  13. #define FLOAT 4
  14. #define TEST 0
  15. #define ACTION 1
  16. #define GLOBAL 1
  17. #define LOCAL 2
  18.  
  19. int create_tmp(FILE*, char*, int);
  20. void firstpass(char*);
  21. void modulepass(char*, int);
  22. void parseglobal(char*);
  23. char *getword(int, char*, char*);
  24. void gotoline(FILE*, int);
  25. int freadln(FILE*, char*);
  26. char *caps(char[]);
  27. char *stripquotes(char[]);
  28. int checkin(char*, char*);
  29. void check_set(char*, char*);
  30. void menubuild(int, char*);
  31. void modulebuild(int, char*);
  32. void def_var(char*, FILE*, int, int);
  33. void findtools(FILE*, int);
  34. void createform(FILE*, FILE*, int, int);
  35. void createprocess(FILE*, FILE*, int, int);
  36. void createoutput(FILE*, FILE*, int, int);
  37. int countword(char*);
  38. int resolve(FILE*, char[], int, int, int);
  39.  
  40. struct sections {
  41.     int global_line;
  42.     int no_menus;
  43.     int no_modules;
  44.     struct menu {
  45.         char name[WORD_SIZE];
  46.         int line;
  47.         } menu[MAX_MENUS];
  48.     struct module {
  49.         char name[WORD_SIZE];
  50.         int line;
  51.         int no_forms;
  52.         int no_process;
  53.         int no_outputs;
  54.         struct form {
  55.             char name[WORD_SIZE];
  56.             int line;
  57.             } form[MAX_MENUS];
  58.         struct process {
  59.             char name[WORD_SIZE];
  60.             int line;
  61.             } process[MAX_MENUS];
  62.         struct output {
  63.             char name[WORD_SIZE];
  64.             int line;
  65.             } output[MAX_MENUS];
  66.  
  67.         } module[MAX_MENUS];
  68.     }; 
  69.  
  70. struct toolbar {
  71.     char name[WORD_SIZE];
  72.     int p_m;
  73.     int a_d;
  74.     char process[WORD_SIZE];
  75.     char module[WORD_SIZE];
  76.     char value[WORD_SIZE];
  77.     int form;
  78.     } toolbar[MAX_MENUS];
  79.  
  80. int no_tools;
  81.  
  82. struct variable {
  83.     char name[WORD_SIZE];
  84.     int type;
  85.     int g_l; 
  86.     int module;
  87.     int process;
  88.     } variable[MAX_MENUS];
  89.  
  90. int no_vars;
  91.  
  92. char pghost[WORD_SIZE];
  93. char pgdatabase[WORD_SIZE];
  94. char pgport[WORD_SIZE];
  95. char pgoptions[WORD_SIZE];
  96. char pgtty[WORD_SIZE];
  97. char cgi_url[WORD_SIZE];
  98. char cgi_dir[WORD_SIZE];
  99. char doc_url[WORD_SIZE];
  100. char doc_dir[WORD_SIZE];
  101. char src_dir[WORD_SIZE];
  102. char pgdir[WORD_SIZE];
  103. char httpd_dir[WORD_SIZE];
  104. int no_lines;
  105. struct sections section;
  106. int cline=0;
  107.  
  108. main (int argc, char *argv[])
  109. {
  110.     FILE *input,*tmp;
  111.     char tmp_filename[MAX_PATH];
  112.     int a;
  113.     if (argc!=2) {
  114.         fprintf(stderr,"Usage: appgen <filename>\n");
  115.         exit(1);
  116.         }
  117.     input=fopen(argv[1],"rt");
  118.     if (input==NULL) {
  119.         fprintf(stderr,"ERROR: Cannot open input file %s\n",argv[1]);
  120.         exit(1);
  121.         }
  122.     fclose(input);
  123.     printf("AppGen Compiler v0.2\nCopyright 1996, Andrew Whaley\nCompiling file %s\n",argv[1]);
  124.     strcpy(tmp_filename,"/tmp/appgen01");
  125.     tmp=fopen(tmp_filename,"wt");
  126.     if (tmp==NULL) {
  127.         fprintf(stderr,"ERROR: Cannot create temporary file %s\n",tmp_filename);
  128.         exit(1);
  129.         }
  130.     create_tmp(tmp,argv[1],1);
  131.     fclose(tmp);
  132.     strcpy(tmp_filename,"/tmp/appgen02");
  133.     tmp=fopen(tmp_filename,"wt");
  134.     if (tmp==NULL) {
  135.         fprintf(stderr,"ERROR: Cannot create temporary file %s\n",tmp_filename);
  136.         exit(1);
  137.         }
  138.     no_lines=create_tmp(tmp,"/tmp/appgen01",2);
  139.     fclose(tmp);
  140.     firstpass("/tmp/appgen02");
  141.     parseglobal("/tmp/appgen02");
  142.     for (a=0; a<section.no_menus; a++) {
  143.         menubuild(a,"/tmp/appgen02");
  144.         }
  145.     for (a=0; a<section.no_modules; a++) {
  146.         modulepass("/tmp/appgen02",a);
  147.         modulebuild(a,"/tmp/appgen02");
  148.         }
  149.     strcpy(tmp_filename,src_dir);
  150.     strcat(tmp_filename,"/makefile");
  151.     tmp=fopen(tmp_filename,"wt");
  152.     if (tmp==NULL) {
  153.         fprintf(stderr,"ERROR: Cannot create makefile %s\n",tmp_filename);
  154.         exit(1);
  155.         }
  156.     fprintf(tmp,"HTTPD=%s\n",httpd_dir);
  157.     fprintf(tmp,"POSTGRES=%s\n",pgdir);
  158.     fprintf(tmp,"APPGEN=/usr/local/AppGEN\n");
  159.     fprintf(tmp,"all:\n");
  160.     for (a=0; a<section.no_modules; a++) {
  161.         fprintf(tmp,"    gcc %s.c -o %s -I$(POSTGRES)/include -I$(APPGEN)/include -L$(POSTGRES)/lib -L$(APPGEN)/lib -lappgen -lpq\n",section.module[a].name,section.module[a].name);
  162.         }
  163.     for (a=0; a<section.no_modules; a++) {
  164.         fprintf(tmp,"    cp %s %s/%s\n",section.module[a].name,cgi_dir,section.module[a].name);
  165.         }
  166.     for (a=0; a<section.no_menus; a++) {
  167.         fprintf(tmp,"    cp %s.html %s/%s.html\n",section.menu[a].name,doc_dir,section.menu[a].name);
  168.         }
  169.     fclose(tmp);
  170.     printf("Compile complete, no errors found.\n");
  171. }
  172.  
  173. void modulebuild(int m, char *inputfile)
  174. {
  175.     FILE *input,*cout;
  176.     char line[LINE_SIZE],word[WORD_SIZE],title[WORD_SIZE];
  177.     char filename[MAX_PATH];
  178.     char set_section[WORD_SIZE];
  179.     char def_process[WORD_SIZE];
  180.     int def=0;
  181.     int elsev=0;
  182.     char type[WORD_SIZE];
  183.     int a,b;
  184.     no_vars=0;
  185.     printf("    Creating module - %s\n",section.module[m].name);
  186.     strcpy(filename,src_dir);
  187.     strcat(filename,"/");
  188.     strcat(filename,section.module[m].name);
  189.     strcat(filename,".c");
  190.     strcpy(set_section,"MODULE ");
  191.     strcat(set_section,section.module[m].name);
  192.     input=fopen(inputfile,"rt");
  193.     if (input==NULL) {
  194.         fprintf(stderr,"ERROR: Cannot open temporary file %s\n",inputfile);
  195.         exit(1);
  196.         }
  197.     cout=fopen(filename,"wt");
  198.     if (cout==NULL) {
  199.         fprintf(stderr,"ERROR: Cannot create menu file %s\n",filename);
  200.         exit(1);
  201.         }
  202.     fprintf(cout,"/* %s created by AppGEN Compiler */\n\n",filename);
  203.     fprintf(cout,"#include <AppGEN.h>\n");
  204.     fprintf(cout,"#include <libpq-fe.h>\n\n");
  205.     for (a=0; a<section.module[m].no_forms; a++) {
  206.         fprintf(cout,"void form_%s(int, PGresult*);\n",section.module[m].form[a].name);
  207.         }
  208.     for (a=0; a<section.module[m].no_process; a++) {
  209.         fprintf(cout,"void process_%s();\n",section.module[m].process[a].name);
  210.         }
  211.     for (a=0; a<section.module[m].no_outputs; a++) {
  212.         fprintf(cout,"void output_%s(PGresult*);\n",section.module[m].output[a].name);
  213.         }
  214.     fprintf(cout,"\nPGconn *ag_sock;\n");
  215.     fprintf(cout,"PGresult* ag_blank;\n");
  216.     fprintf(cout,"PGresult* ag_result;\n");
  217.     fprintf(cout,"\nvoid main(int argc, char *argv[])\n");
  218.     fprintf(cout,"{\n");
  219. /* Global Variables */
  220.     gotoline(input,section.module[m].line+1);
  221.     freadln(input,line);
  222.     while ((line[0]!='[') && (!feof(input))) {
  223.         if (strcmp(getword(1,line,word),"SET")==0) {
  224.             check_set(line,set_section);
  225.             if (strcmp(getword(2,line,word),"default_process")==0) {
  226.                 strcpy(def_process,stripquotes(getword(4,line,word)));
  227.                 def=1;
  228.                 }
  229.             }
  230.         else def_var(line,cout,m,999);
  231.         freadln(input,line);    
  232.         }
  233.  
  234.     fprintf(cout,"    ag_sock=PQsetdb(\"%s\",NULL,NULL,NULL,\"%s\");\n",pghost,pgdatabase);
  235.     fprintf(cout,"    if (PQstatus(ag_sock)==CONNECTION_BAD) {\n");
  236.     fprintf(cout,"        printf(\"Content-Type: text/plain\\n\\nError: Cannot connect to Postgres95.\\n\");\n");
  237.     fprintf(cout,"        exit(0);\n");
  238.     fprintf(cout,"        }\n");
  239. /* TOOLBARS : */
  240.     findtools(input,m);
  241.     for (a=0; a<no_tools; a++) {
  242.         if (elsev==1) fprintf(cout,"    else ");
  243.         else fprintf(cout,"    "); 
  244.         fprintf(cout,"if (strcmp(\"\'%s\'\",ag_parse(ag_buff,\"AG_PROCESS\",0))==0) ",toolbar[a].name);
  245.         if (toolbar[a].p_m==1) fprintf(cout,"process_%s();\n",toolbar[a].process);
  246.         if (toolbar[a].p_m==2) fprintf(cout,"ag_get_html(\"%s/%s.html\");\n",doc_dir,toolbar[a].process);
  247.         if (toolbar[a].p_m==3) {
  248.             fprintf(cout,"ag_jump_module(\"%s/%s\",\"%s\",\"%s\",ag_parse(ag_buff,\"%s\",1));\n",cgi_dir,toolbar[a].module,caps(toolbar[a].process),toolbar[a].value,toolbar[a].value);
  249.             }
  250.         elsev=1;
  251.         }
  252.     for (a=0; a<section.module[m].no_process; a++) {
  253.         if (elsev==1) fprintf(cout,"    else ");
  254.         else fprintf(cout,"    "); 
  255.         fprintf(cout,"if (strcmp(\"\'%s\'\",ag_parse(ag_buff,\"AG_PROCESS\",0))==0) ",caps(section.module[m].process[a].name));
  256.         fprintf(cout,"process_%s();\n",section.module[m].process[a].name);
  257.         elsev=1;
  258.         }
  259.     if (elsev==1) fprintf(cout,"    else ");
  260.     else fprintf(cout,"    "); 
  261.     fprintf(cout,"if (strcmp(\"\'AG_ABOUT\'\",ag_parse(ag_buff,\"AG_PROCESS\",0))==0) ag_about();\n");
  262.     if (def==1) {
  263.         fprintf(cout,"    else process_%s();\n",def_process);
  264.         }
  265.     fprintf(cout,"    PQfinish(ag_sock);\n");
  266.     fprintf(cout,"}\n\n");
  267. /* FORMS */
  268.     for (a=0; a<section.module[m].no_forms; a++) {
  269.         createform(input,cout,m,a);    
  270.         }
  271.     for (a=0; a<section.module[m].no_process; a++) {
  272.         createprocess(input,cout,m,a);
  273.         }
  274.     for (a=0; a<section.module[m].no_outputs; a++) {
  275.         createoutput(input,cout,m,a);
  276.         }
  277.     fclose(cout);
  278. }
  279.  
  280. void createprocess(FILE *input, FILE *cout, int m, int f)
  281. {
  282.     int a,b,c,d,i,mw,nc;
  283.     int no_datas=0;
  284.     char datas[MAX_MENUS][WORD_SIZE];
  285.     char invar[WORD_SIZE],index[WORD_SIZE];
  286.     char x[WORD_SIZE],y[WORD_SIZE];
  287.     char word[WORD_SIZE],line[LINE_SIZE],set_section[WORD_SIZE],worda[WORD_SIZE];
  288.     fprintf(cout,"void process_%s()\n{\n",section.module[m].process[f].name);
  289.     fprintf(cout,"    char ag_query[4096];\n");
  290.     gotoline(input,section.module[m].process[f].line+1);
  291.     freadln(input,line);
  292.     while((line[0]!='[')&&(!feof(input))) {    
  293. /* Variables */
  294.         if (strcmp(getword(1,line,word),"DATA")==0) {
  295.             def_var(line,cout,m,f);
  296.             strcpy(datas[no_datas],getword(2,line,word));
  297.             no_datas++;
  298.             }
  299.         if (strcmp(getword(1,line,word),"INT")==0) def_var(line,cout,m,f);
  300.         if (strcmp(getword(1,line,word),"STRING")==0) def_var(line,cout,m,f);
  301. /* DISPLAY form */
  302.         if (strcmp(getword(1,line,word),"DISPLAY")==0) {
  303.             b=countword(line);
  304.             if (b==2) fprintf(cout,"    form_%s(AG_NEW,ag_blank);\n",getword(2,line,word));
  305.             else if (b==4) {
  306.                 if (strcmp(getword(4,line,word),"input")==0) fprintf(cout,"    form_%s(AG_PARSE,ag_blank);\n",getword(2,line,worda));
  307.                 else fprintf(cout,"    form_%s(AG_FULL,%s);\n",getword(2,line,word),getword(4,line,worda));
  308.                 }
  309.             else {
  310.  
  311.                 fprintf(cout,"    ag_errors[%s].error=1;\n",getword(8,line,word));
  312.                 fprintf(cout,"    strcpy(ag_errors[%s].message,%s);\n",getword(8,line,worda),getword(6,line,word));
  313.                 if (strcmp(getword(4,line,word),"input")==0) fprintf(cout,"    form_%s(AG_ERROR,ag_blank);\n",getword(2,line,worda));
  314.                 else fprintf(cout,"    form_%s(AG_FULL,%s);\n",getword(2,line,word),getword(4,line,worda));
  315.                 }
  316.             }
  317. /* MOVE */
  318.         if (strcmp(getword(1,line,word),"MOVE")==0) {
  319.             for (a=0; a<no_vars; a++) if (strcmp(getword(4,line,word),variable[a].name)==0) {
  320.                 if (variable[a].type==INT) fprintf(cout,"    %s=%s;\n",variable[a].name,getword(2,line,word));
  321.                 if (variable[a].type==STRING) fprintf(cout,"    strcpy(%s,%s);\n",variable[a].name,getword(2,line,word));
  322.                 if (variable[a].type==DATA) {
  323.                     fprintf(stderr,"ERROR: Can't MOVE to DATA variable type.\n");
  324.                     exit(1);
  325.                     }
  326.                 }
  327.             }
  328. /* LEFT */     if (strcmp(getword(1,line,word),"LEFT")==0) {
  329.             getword(4,line,worda);
  330.             if (resolve(cout, worda, m, f, TEST)!=STRING) {
  331.                         fprintf(stderr,"Can't LEFT to non-string variable: %s\n",worda);
  332.                         exit(1);
  333.                         }
  334.             getword(2,line,word);
  335.             fprintf(cout,"    strcpy(ag_temp_string,");
  336.             resolve(cout,word,m,f,ACTION);
  337.             fprintf(cout,");\n");
  338.             fprintf(cout,"    for (ag_i=0; ag_i<%s; ag_i++) %s[ag_i]=ag_temp_string[ag_i];\n",getword(5,line,word),worda);
  339.             fprintf(cout,"    %s[%s]=0;\n",worda,getword(5,line,word));
  340.                 
  341.             
  342.             }            
  343.  
  344. /* OUTPUT */
  345.         if (strcmp(getword(1,line,word),"OUTPUT")==0) {
  346.             fprintf(cout,"    output_%s(%s);\n",getword(2,line,word),getword(4,line,worda));
  347.             }
  348. /* ROWS */
  349.         if (strcmp(getword(1,line,word),"ROWS")==0) {
  350.             fprintf(cout,"    %s=PQntuples(%s);\n",getword(4,line,word),getword(2,line,worda));
  351.             }
  352. /* IF */
  353.         if (strcmp(getword(1,line,word),"IF")==0) {
  354.             b=countword(line);
  355.             for (a=1; a<=b; a++) {
  356.                 if (strcmp(getword(a,line,word),"THEN")==0) c=a;
  357.                 }
  358.             fprintf(cout,"    if (");
  359.             for (a=2; a<c; a++) {
  360.                 getword(a,line,worda);
  361.                 if (strcmp(worda,"NOT")==0) fprintf(cout,"!");
  362.                 else if ((worda[0]=='B')&&(worda[1]=='L')) {
  363.                     fprintf(cout,"(strlen(");
  364.                     if ((worda[6]=='i') && (worda[7]=='n') && (worda[8]=='p') && (worda[9]=='u') && (worda[10]=='t')) {
  365.                         i=0;
  366.                         for (b=12; ((b<strlen(worda))&&(worda[b]!=')')); b++) {
  367.                             invar[i]=worda[b];
  368.                             i++;
  369.                             }
  370.                         invar[i]=0;
  371.                         fprintf(cout,"ag_parse(ag_buff,\"%s\",0)",caps(invar));            
  372.                         }    
  373.                     else {
  374.                         for (b=6; ((b<strlen(worda))&&(worda[b]!=')')); b++) fprintf(cout,"%c",worda[b]);
  375.                         }
  376.                     fprintf(cout,")<1)");
  377.         
  378.                     }
  379.                 else if ((worda[0]=='i') && (worda[1]=='n') && (worda[2]=='p') && (worda[3]=='u') && (worda[4]=='t')) {
  380.                     i=0;
  381.                     for (b=6; ((c<strlen(worda))&&(worda[b]!=' ')); b++) {
  382.                         invar[i]=worda[b];
  383.                         i++;
  384.                         }
  385.                     invar[i]=0;    
  386.                     fprintf (cout,"atoi(ag_parse(ag_buff,\"%s\",1))",caps(invar));
  387.                     }
  388.                 else {
  389.                     if (worda[0]=='=') fprintf(cout,"=");
  390.                     fprintf(cout,"%s ",worda);
  391.                     }
  392.                 }
  393.             fprintf(cout,") ");
  394.             }
  395. /* PROCESS */
  396.         if (strcmp(getword(1,line,word),"PROCESS")==0) {
  397.             fprintf(cout,"    process_%s();\n",getword(2,line,word));
  398.             }
  399.  
  400.  
  401. /* FOR */    if (strcmp(getword(1,line,word),"FOR")==0) {
  402.             strcpy(index,getword(2,line,word));
  403.             strcpy(invar,getword(6,line,word));
  404.             fprintf(cout,"    for (%s=%s; %s<=%s; %s++)\n",index,getword(4,line,word),index,invar,index);
  405.             }
  406.  
  407.  
  408. /* BEGIN */
  409.         if (strcmp(getword(1,line,word),"BEGIN")==0) {
  410.             fprintf(cout,"    {\n");
  411.             }
  412.  
  413.  
  414. /* END */
  415.         if (strcmp(getword(1,line,word),"END")==0) {
  416.             fprintf(cout,"    }\n");
  417.             }
  418. /* SYSDATE */
  419.         if (strcmp(getword(1,line,word),"SYSDATE")==0) {
  420.             fprintf(cout,"    ag_sysdate(%s);\n",getword(2,line,word));
  421.             if (countword(line)>2) fprintf(cout,"    ag_systime(%s);\n",getword(3,line,word));
  422.             }
  423. /* SHOWLN */
  424.         if (strcmp(getword(1,line,word),"SHOWLN")==0) {
  425.             nc=countword(line);
  426.             for (a=2; a<nc; a++) {
  427.                 getword(a,line,worda);                
  428.                 if (worda[0]=='\"') fprintf(cout,"    printf(%s);\n",worda);
  429.                 else {
  430.                     if ((resolve(cout, worda, m, f, TEST)==INT)||(resolve(cout, worda, m, f, TEST)==FLOAT)) {
  431.                         fprintf (cout,"    printf(\"%%d\",");
  432.                         }
  433.                     else fprintf (cout," printf(\"%%s\",");
  434.                     resolve(cout,worda, m, f, ACTION);
  435.                     fprintf(cout,");\n");
  436.                     }    
  437.                 }
  438.             fprintf(cout,"    printf(\"\\n\");\n");
  439.             }
  440.                     
  441. /* CALC */
  442.         if (strcmp(getword(1,line,word),"CALC")==0) {
  443.             b=999;
  444.             c=countword(line);
  445.             for (a=0; a<no_vars; a++) if (strcmp(variable[a].name,getword(c,line,word))==0) b=a;
  446.             if (b==999) { 
  447.                 fprintf(stderr,"ERROR: Variable not found %s\n",getword(c,line,word));
  448.                 exit(1);
  449.                 }
  450.             if (variable[b].type!=INT) {
  451.                 fprintf(stderr,"ERROR: Variable %s is not of type INT in CALC.\n",getword(c,line,word));
  452.                 exit(1);
  453.                 }
  454.             fprintf(cout,"    %s=(",variable[b].name);
  455.             mw=c-2;
  456.             for (a=2; a<=mw; a++) {
  457.                 getword(a,line,worda);
  458.                 if ((worda[0]=='i') && (worda[1]=='n') && (worda[2]=='p') && (worda[3]=='u') && (worda[4]=='t')) {
  459.                     i=0;
  460.                     for (c=6; ((c<strlen(worda))&&(worda[c]!=' ')); c++) {
  461.                         invar[i]=worda[c];
  462.                         i++;
  463.                         }
  464.                     invar[i]=0;    
  465.                     fprintf (cout,"atoi(ag_parse(ag_buff,\"%s\",1)))",caps(invar));
  466.                     }
  467.                 else if ((worda[strlen(worda)-2]=='.')||(worda[strlen(worda)-3]=='.')) {
  468.                     i=0;
  469.                     for (c=0; ((c<strlen(worda))&&(worda[c]!='.')); c++) {
  470.                         invar[i]=worda[c];
  471.                         i++;
  472.                         }
  473.                     invar[i]=0;
  474.                     i=0;
  475.                     for (c=c+1; ((c<strlen(worda))&&(worda[c]!='.')); c++) {
  476.                         x[i]=worda[c];
  477.                         i++;
  478.                         }
  479.                     x[i]=0;
  480.                     i=0;
  481.                     for (c=c+1; ((c<strlen(worda))&&(worda[c]!='.')); c++) {
  482.                         y[i]=worda[c];
  483.                         i++;
  484.                         }
  485.                     y[i]=0;
  486.                     fprintf (cout,"atoi(PQgetvalue(%s,%s,%s))",invar,x,y);
  487.                     }    
  488.  
  489.                 else fprintf(cout,"%s ",worda);
  490.                 }
  491.             fprintf(cout,");\n");
  492.  
  493.             }
  494.  
  495. /* APPEND */
  496.         if (strcmp(getword(1,line,word),"APPEND")==0) {
  497.             b=countword(line);
  498.             getword(2,line,worda);
  499.             for (a=0; a<b-3; a++) {
  500.                 fprintf(cout,"    strcat(%s,",worda);
  501.                 getword(3+a,line,word);
  502.                 if (word[0]=='\"') fprintf(cout,"%s);\n",word);
  503.                 else if ((word[0]=='i') && (word[1]=='n') && (word[2]=='p') && (word[3]=='u') && (word[4]=='t')) {
  504.                     i=0;
  505.                     for (c=a+5; ((c<strlen(word))&&(word[c]!=' ')); c++) {
  506.                         invar[i]=word[c];
  507.                         i++;
  508.                         }
  509.                     invar[i]=0;    
  510.                     fprintf (cout,"ag_parse(ag_buff,\"%s\",1));\n",caps(invar));
  511.                     }
  512.                 }
  513.             }
  514.  
  515. /* SQL */
  516.         if (strcmp(getword(1,line,word),"SQL")==0) {
  517.             getword(2,line,worda);
  518.             if (worda[0]=='\"') {
  519.                 fprintf(cout,"    strcpy(ag_query,\"");
  520.                 for (a=1; a<strlen(worda); a++) { 
  521.                     if ((worda[a]!='$') && (worda[a]!='\"')) fprintf(cout,"%c",worda[a]);
  522.                     if (worda[a]=='$') {
  523.                         fprintf(cout,"\");\n");
  524.                         fprintf(cout,"    strcat(ag_query,");
  525.                         a++;
  526.                         if ((worda[a]=='i') && (worda[a+1]=='n')) {
  527.                             i=0;
  528.                             for (b=a+6; ((b<strlen(worda)) && (worda[b]!=' ') && (worda[b]!=',') && (worda[b]!=';') && (worda[b]!=')') && (worda[b]!='\'')); b++) {
  529.                                 invar[i]=worda[b];
  530.                                 i++;
  531.                                 }
  532.                             invar[i]=0;
  533.                             a=a+i+5;
  534.                             i=0;
  535.                             for (b=0; ((b<strlen(invar))&&(invar[b]!='.')); b++); 
  536.                             if (b==strlen(invar)) {    
  537.                                 fprintf(cout,"ag_parse(ag_buff,\"%s\",1));\n",caps(invar));
  538.                                 fprintf(cout,"    strcat(ag_query,\"");
  539.                                 }
  540.                             else {
  541.                                 d=b;
  542.                                 b++;
  543.                                 while (b<strlen(invar)) {
  544.                                     index[i]=invar[b];
  545.                                     i++;
  546.                                     b++;
  547.                                     }
  548.                                 invar[d]=0;
  549.                                 index[i]=0;    
  550.                                 fprintf(cout,"ag_parse(ag_buff,ag_sprintf(ag_buff,\"%s_%%d\",%s),1));\n",caps(invar),index);
  551.                                 fprintf(cout,"    strcat(ag_query,\"");    
  552.                                 }        
  553.                             }
  554.                         else {
  555.                             i=0;
  556.                             for (b=a; ((b<strlen(worda)) && (worda[b]!=' ') && (worda[b]!=',') && (worda[b]!=';') && (worda[b]!=')') && (worda[b]!='\'')); b++) {
  557.                                 invar[i]=worda[b];
  558.                                 i++;
  559.                                 }
  560.                             invar[i]=0;
  561.                             b=999;
  562.                             for (c=0; c<no_vars; c++) if (strcmp(invar,variable[c].name)==0) b=c;
  563.                             if (b==999) {
  564.                                 fprintf(stderr,"ERROR: Variable %s not found in SQL.\n",invar);
  565.                                 exit(1);
  566.                                 }
  567.                             if (variable[b].type==INT) 
  568.                             fprintf(cout,"ag_itoa(ag_buff,%s));\n",invar);
  569.                             else {
  570.                                 fprintf(cout,"\"\");\n"); 
  571.                                 fprintf(cout,"    strcat(ag_query,%s);\n",invar);
  572.                                 fprintf(cout,"    strcat(ag_query,\"\");\n"); 
  573.                                 } 
  574.                             fprintf(cout,"    strcat(ag_query,\"");
  575.                             a=a+i-1;
  576.                             }
  577.                         }
  578.                     if (worda[a]=='\"') {
  579.                         fprintf(cout,"\");\n");
  580.                         }
  581.                     }
  582.                 }
  583.             if (countword(line)!=4) fprintf(cout,"    ag_result=");
  584.             else fprintf(cout,"    %s=",getword(4,line,word));
  585.             getword(2,line,worda);
  586.             if (worda[0]=='\"') fprintf(cout,"PQexec(ag_sock,ag_query);\n");
  587.             else fprintf(cout,"PQexec(ag_sock,%s);\n",getword(2,line,word));
  588.             if (countword(line)!=4) fprintf(cout,"    if (ag_result==NULL) ag_error(PQerrorMessage(ag_sock),");
  589.             else fprintf(cout,"    if (%s==NULL) ag_error(PQerrorMessage(ag_sock),",getword(4,line,word));
  590.             if (worda[0]=='\"') fprintf(cout,"ag_query);\n");
  591.             else fprintf(cout,"%s);\n",getword(2,line,word));
  592.             }
  593.  
  594.         
  595.         freadln(input,line);
  596.         }
  597.     for (a=0; a<no_datas; a++) fprintf(cout,"    PQclear(%s);\n",datas[a]);
  598.     fprintf(cout,"}\n\n");
  599. }
  600.  
  601. int resolve(FILE *cout, char var[WORD_SIZE], int m, int p, int action)
  602. {
  603.     int a,b,c,i;
  604.     char invar[WORD_SIZE],x[WORD_SIZE],y[WORD_SIZE];
  605.  
  606. /* INPUT TYPE */
  607.  
  608.     if ((var[0]=='i') && (var[1]=='n') && (var[2]=='p') && (var[3]=='u') && (var[4]=='t')) {
  609.                 i=0;
  610.                 for (c=6; ((c<strlen(var))&&(var[c]!=' ')); c++) {
  611.                 invar[i]=var[c];
  612.                 i++;
  613.                 }
  614.             invar[i]=0;    
  615.             if (action==ACTION) fprintf (cout,"ag_parse(ag_buff,\"%s\",1)",caps(invar));
  616.             return(STRING);            
  617.             }
  618.  
  619. /* DATA TYPE WITH INDEX */ 
  620.  
  621.     else if ((var[strlen(var)-2]=='.')||(var[strlen(var)-3]=='.')) {
  622.                     i=0;
  623.                     for (c=0; ((c<strlen(var))&&(var[c]!='.')); c++) {
  624.                         invar[i]=var[c];
  625.                         i++;
  626.                         }
  627.                     invar[i]=0;
  628.                     i=0;
  629.                     for (c=c+1; ((c<strlen(var))&&(var[c]!='.')); c++) {
  630.                         x[i]=var[c];
  631.                         i++;
  632.                         }
  633.                     x[i]=0;
  634.                     i=0;
  635.                     for (c=c+1; ((c<strlen(var))&&(var[c]!='.')); c++) {
  636.                         y[i]=var[c];
  637.                         i++;
  638.                         }
  639.                     y[i]=0;
  640.                     if (action=ACTION) fprintf (cout,"PQgetvalue(%s,%s,%s)",invar,x,y);
  641.                     return(STRING);
  642.                     }    
  643.  
  644.  
  645.  
  646.  
  647.  
  648. /* REGULAR VARIABLES */
  649.  
  650.     else { 
  651.             b=999;
  652.             for (c=0; c<no_vars; c++) if (strcmp(variable[c].name,var)==0) b=c;
  653.                 if (b==999) { 
  654.                     fprintf(stderr,"ERROR: Variable \"%s\" not found in MODULE %s PROCESS %s\n",var,section.module[m].name,section.module[m].process[p].name);
  655.                     exit(1);
  656.                     }
  657.             if (action==ACTION) fprintf (cout,"%s",var);    
  658.             return (variable[b].type); 
  659.             }
  660.     
  661. }    
  662.  
  663.  
  664. void createoutput(FILE *input, FILE *cout, int m, int f)
  665. {
  666.     int a,b;
  667.     struct field {
  668.         char name[WORD_SIZE];
  669.         char align[WORD_SIZE];
  670.         char description[WORD_SIZE];
  671.         int  total;
  672.         } field[WORD_SIZE];
  673.     char word[WORD_SIZE],line[LINE_SIZE],set_section[WORD_SIZE],worda[WORD_SIZE],title[WORD_SIZE];
  674.     char type[WORD_SIZE];
  675.     int screen=1;
  676.     int graph=0;
  677.     int title_yn=0;
  678.     int link_yn=0;
  679.     int total_yn=0;
  680.     int col;
  681.     int once=0;
  682.     int total[MAX_MENUS];
  683.     char link[WORD_SIZE];
  684.     fprintf(cout,"void output_%s(PGresult *ag_res)\n{\n",section.module[m].output[f].name);
  685.     fprintf(cout,"    int a,b,n_cols,n_rows;\n");
  686.     fprintf(cout,"    int total[%d];\n",MAX_MENUS);
  687.     fprintf(cout,"    char ag_args[1024];\n");
  688.     fprintf(cout,"    for (a=0; a<%d; a++) total[a]=0;\n",MAX_MENUS);
  689.     fprintf(cout,"    strcpy(ag_args,getenv(\"QUERY_STRING\"));\n");
  690.     fprintf(cout,"    printf(\"Content-Type: text/html\\n\\n\");\n");
  691.     strcpy(set_section,"OUTPUT ");
  692.     strcat(set_section,section.module[m].output[f].name);
  693.     strcat(set_section," in MODULE ");
  694.     strcat(set_section,section.module[m].name);
  695.     gotoline(input,section.module[m].output[f].line+1);
  696.     freadln(input,line);
  697.     while((line[0]!='[')&&(!feof(input))) {
  698.         if (strcmp(getword(1,line,word),"SET")==0) {
  699.             if (strcmp(getword(2,line,word),"title")==0) {
  700.                 strcpy(title,stripquotes(getword(4,line,word)));
  701.                 title_yn=1;
  702.                 if (title_yn==1) fprintf(cout,"    printf(\"<HTML><TITLE>%s</TITLE>\\n\");\n",title);
  703.                 fprintf(cout,"    printf(\"<BODY BACKGROUND=\\\"/ag/ag_texture.gif\\\">\\n\");\n");
  704.                 if (title_yn==1) fprintf(cout,"    printf(\"<H1>%s</H1>\\n\");\n",title);
  705.                 fprintf(cout,"    printf(\"<HR>\\n\");\n");
  706.                 once=1;
  707.                 }
  708.             if (strcmp(getword(2,line,word),"type")==0) {
  709.                 strcpy(type,stripquotes(getword(4,line,word)));
  710.                 if (strcmp(type,"screen")==0) {
  711.                     screen=1;
  712.                     graph=0;
  713.                     }
  714.                 if (strcmp(type,"graph")==0) {
  715.                     screen=0;
  716.                     graph=1;
  717.                     }    
  718.                 }    
  719.             }
  720.         else if (once==0) {
  721.             fprintf(cout,"    printf(\"<HTML><TITLE>%s</TITLE>\\n\");\n",section.module[m].output[f].name);
  722.             fprintf(cout,"    printf(\"<BODY>\\n\");\n");
  723.             fprintf(cout,"    printf(\"<H1>%s</H1>\\n\");\n",section.module[m].output[f].name);
  724.             fprintf(cout,"    printf(\"<HR>\\n\");\n");
  725.             once=1;
  726.             }
  727.         if (strcmp(getword(1,line,word),"PROCESS")==0) {
  728.             fprintf(cout,"    process_%s();\n",getword(2,line,word));
  729.             }
  730.  
  731.  
  732.  
  733.         if ((strcmp(getword(1,line,word),"DEFINE")==0)&&(strcmp(getword(2,line,worda),"OUTPUT")==0)) {
  734.             freadln(input,line);
  735.             b=0;
  736.             while(line[0]!='}') {
  737.                 strcpy(field[b].name,getword(1,line,word));
  738.                 strcpy(field[b].align,getword(3,line,word));
  739.                 strcpy(field[b].description,stripquotes(getword(5,line,word)));
  740.                 field[b].total=0;
  741.                 if (countword(line)>6) {
  742.                     field[b].total=1;
  743.                     total_yn=1;
  744.                     }
  745.                 b++;
  746.                 freadln(input,line);
  747.                 }
  748.             if ((line[0]=='}') && (countword(line)>2)) {
  749.                 link_yn=1;
  750.                 strcpy(link,cgi_url);
  751.                 strcat(link,"/");
  752.                 strcat(link,section.module[m].name);
  753.                 strcat(link,"?AG_PROCESS=");
  754.                 strcat(link,caps(getword(5,line,word)));
  755.                 strcat(link,"&");
  756.                 strcat(link,caps(getword(8,line,word)));
  757.                 for (a=0; a<b; a++) if (strcmp(field[a].name,getword(8,line,word))==0) col=a;
  758.                 }    
  759.             if (screen) {
  760.                 fprintf(cout,"    printf(\"<TABLE BORDER=\\\"1\\\">\\n\");\n");
  761.                 fprintf(cout,"    printf(\"<TR>");
  762.                 for (a=0; a<b; a++) {
  763.                     fprintf(cout,"<TH>%s",field[a].description);
  764.                     }
  765.                 fprintf(cout,"\\n\");\n");
  766.                 }
  767.             if (graph) {
  768.                 fprintf(cout,"    printf(\"<APPLET CODEBASE=\\\"/ag\\\" CODE=\\\"graph.class\\\" WIDTH=400 HEIGHT=300>\\n\");\n");
  769.                 }    
  770.             fprintf(cout,"    n_rows=PQntuples(ag_res);\n");
  771.             if (graph) fprintf(cout,"    printf(\"<PARAM NAME=rows VALUE=%%d>\\n\",n_rows);\n");
  772.             fprintf(cout,"    n_cols=%d;\n",b);
  773.             fprintf(cout,"    for (a=0; a<n_rows; a++) {\n");
  774.             if (screen) {
  775.                 fprintf(cout,"        printf(\"<TR>\");\n");
  776.                 for (a=0; a<b; a++) {
  777.                     if (link_yn) {
  778.                         if (a==col) fprintf(cout,"            printf(\"<TD ALIGN=\\\"%s\\\"><A HREF=%s=%%s&%%s>%%s</A>\",ag_encode(ag_buff,PQgetvalue(ag_res,a,%d)),ag_args,PQgetvalue(ag_res,a,%d));\n",field[a].align,link,a,a);
  779.                         else fprintf(cout,"             printf(\"<TD ALIGN=\\\"%s\\\">%%s\",PQgetvalue(ag_res,a,%d));\n",field[a].align,a);    
  780.                         }
  781.                     else fprintf(cout,"            printf(\"<TD ALIGN=\\\"%s\\\">%%s\",PQgetvalue(ag_res,a,%d));\n",field[a].align,a);
  782.                     if (total_yn) if (field[a].total) fprintf(cout,"    total[%d] += atoi(PQgetvalue(ag_res,a,%d));\n",a,a);
  783.                     fprintf(cout,"        printf(\"\\n\");\n");
  784.                     }
  785.                 }
  786.             if (graph) {
  787.                 fprintf(cout,"             printf(\"<PARAM NAME=x%%d VALUE=%%s>\\n\",a+1,PQgetvalue(ag_res,a,0));\n");    
  788.                 fprintf(cout,"             printf(\"<PARAM NAME=y%%d VALUE=%%s>\\n\",a+1,PQgetvalue(ag_res,a,1));\n");    
  789.                 }
  790.                 
  791.                     
  792.             fprintf(cout,"        }\n");        
  793.             
  794.             if ((screen)&&(total_yn)) {
  795.                 fprintf(cout,"    printf(\"<TR><TR>\");\n");
  796.                 for (a=0; a<b; a++) {
  797.                     fprintf(cout,"    printf(\"<TH ALIGN=\\\"%s\\\">\");\n",field[a].align);
  798.                     if (field[a].total) fprintf(cout,"    printf(\"%%d\",total[%d]);\n",a);
  799.                     }
  800.                 }    
  801.             if (screen) fprintf(cout,"    printf(\"</TABLE>\\n\");\n");
  802.             if (graph) fprintf(cout,"    printf(\"</APPLET>\\n\");\n");
  803.     
  804.  
  805.             }
  806.         freadln(input,line);    
  807.         }
  808.  
  809.     fprintf(cout,"    printf(\"</BODY></HTML>\\n\");\n");
  810.     fprintf(cout,"    PQclear(ag_res);\n");
  811.     fprintf(cout,"    exit(0);\n");
  812.     fprintf(cout,"}\n\n");
  813. }
  814.  
  815.  
  816. void createform(FILE *input, FILE *cout, int m, int f)
  817. {
  818.     int a,b,c,d,i;
  819.     char title[WORD_SIZE],word[WORD_SIZE],line[LINE_SIZE],set_section[WORD_SIZE],worda[WORD_SIZE];
  820.     int title_yn=0;
  821.     int no_forms=0;
  822.     int fc=0;
  823.     struct form {
  824.         char type[WORD_SIZE];
  825.         int rows;
  826.         int fields; 
  827.         struct field {
  828.             char name[WORD_SIZE];
  829.             char description[WORD_SIZE];
  830.             char length[WORD_SIZE];
  831.             char type[WORD_SIZE];
  832.             } field[MAX_MENUS];
  833.         } form[MAX_FORMS];
  834.             
  835.     struct choices {
  836.         int no;
  837.         char val[MAX_MENUS][WORD_SIZE];
  838.         } choices[MAX_FORMS][MAX_MENUS];
  839.         
  840.     fprintf(cout,"void form_%s(int ag_data, PGresult* ag_res)\n{\n",section.module[m].form[f].name);
  841.     fprintf(cout,"    printf(\"Content-Type: text/html\\n\\n\");\n");
  842.  
  843.     strcpy(set_section,"FORM ");
  844.     strcat(set_section,section.module[m].form[f].name);
  845.     strcat(set_section," in MODULE ");
  846.     strcat(set_section,section.module[m].name);
  847.     gotoline(input,section.module[m].form[f].line+1);
  848.     freadln(input,line);
  849.     while((line[0]!='[')&&(!feof(input))) {
  850.         if (strcmp(getword(1,line,word),"SET")==0) {
  851.             check_set(line,set_section);
  852.             if (strcmp(getword(2,line,word),"title")==0) {
  853.                 strcpy(title,stripquotes(getword(4,line,word)));
  854.                 title_yn=1;
  855.                 }
  856.             }
  857.         if ((strcmp(getword(1,line,word),"DEFINE")==0)&&(strcmp(getword(2,line,worda),"FORM")==0)) {
  858.             freadln(input,line);
  859.             b=0;
  860.             while(line[0]!='}') {
  861.                 strcpy(form[no_forms].field[b].name,getword(1,line,word));
  862.                 strcpy(form[no_forms].field[b].description,stripquotes(getword(5,line,word)));
  863.                 strcpy(form[no_forms].field[b].length,getword(3,line,word));
  864.                 strcpy(form[no_forms].field[b].type,getword(7,line,word));
  865.                 if (strcmp(form[no_forms].field[b].type,"CHOICE")==0) {
  866.                     i=countword(line);
  867.                     for (c=0; c<i-7; c++) strcpy(choices[no_forms][b].val[c],stripquotes(getword(c+8,line,word)));
  868.                     choices[no_forms][b].no=c-1;
  869.                     }
  870.                 if (strcmp(form[no_forms].field[b].type,"PICK")==0) {
  871.                     i=countword(line);
  872.                     strcpy(choices[no_forms][b].val[0],stripquotes(getword(9,line,word)));
  873.                     }                        
  874.                 if (strcmp(form[no_forms].field[b].type,"SEQUENCE")==0) {
  875.                     i=countword(line);
  876.                     if (i > 12) {
  877.                         for(c=0; c<i-12; c++) {
  878.                             strcpy(choices[no_forms][b].val[c],stripquotes(getword(c+12,line,word)));
  879.                             }
  880.                         choices[no_forms][b].no=c-1;
  881.                         }
  882.                     else choices[no_forms][b].no=0;
  883.                     }
  884.                 b++;
  885.                 freadln(input,line);
  886.                 }
  887.             form[no_forms].fields=b;    
  888.             strcat(form[no_forms].type,getword(3,line,word));
  889.             if (strcmp(form[no_forms].type,"TABLE")==0) form[no_forms].rows=atoi(getword(5,line,word));
  890.             no_forms++;    
  891.             }
  892.         freadln(input,line);
  893.         }        
  894.     if (title_yn==1) fprintf(cout,"    printf(\"<HTML><TITLE>%s</TITLE>\\n\");\n",title);
  895.     else fprintf(cout,"    printf(\"<HTML><TITLE>%s</TITLE>\\n\");\n",section.module[m].form[f].name);
  896.     fprintf(cout,"    printf(\"<BODY BACKGROUND=\\\"/ag/ag_texture.gif\\\">\\n\");\n");
  897.     fprintf(cout,"    if (ag_data==AG_ERROR) printf(\"<H1>* ERRORS FOUND *</H1>\\n\");\n");
  898.     if (title_yn==1) fprintf(cout,"    else printf(\"<H1>%s</H1>\\n\");\n",title);
  899.     else fprintf(cout,"    else printf(\"<H1>%s</H1>\\n\");\n",section.module[m].form[f].name);
  900.     fprintf(cout,"    printf(\"<HR>\\n\");\n");
  901.     fprintf(cout,"    printf(\"<FORM ACTION=\\\"%s/%s\\\">\\n\");\n",cgi_url,section.module[m].name);
  902.     for (d=0; d<no_forms; d++) {    
  903.         fprintf(cout,"    printf(\"<TABLE>\\n\");\n");
  904.         if (form[d].type[0]=='T') {
  905.             fprintf(cout,"    printf(\"<TR>");
  906.             for (a=0; a<form[d].fields; a++) fprintf(cout,"<TH>%s",form[d].field[a].description);
  907.             fprintf(cout,"\");\n");
  908.             for (b=0; b<form[d].rows; b++) {
  909.                 fprintf(cout,"    printf(\"<TR>\");\n");
  910.                 for (a=0; a<form[d].fields; a++) {
  911.                     if (strcmp(form[d].field[a].type,"CHOICE")==0) {
  912.                         fprintf(cout,"    printf(\"<TD><SELECT NAME=\\\"%s_%d\\\">\\n\");\n",caps(form[d].field[a].name),b);
  913.                         for (c=0; c<choices[d][a].no; c++) {
  914.                             fprintf(cout,"    printf(\"<OPTION VALUE=\\\"%d\\\"\");\n",c);
  915.                             fprintf(cout,"    if (ag_data==AG_FULL) if (atoi(PQgetvalue(ag_res,%d,%d))==%d) printf(\" SELECTED\");\n",b,fc+a,c);
  916.                             fprintf(cout,"    if (ag_data>AG_FULL) if (atoi(ag_parse(ag_buff,\"%s_%d\",1))==%d) printf(\" SELECTED\");\n",caps(form[d].field[a].name),b,c);
  917.                             fprintf(cout,"    printf(\">%s\");\n",choices[d][a].val[c]);
  918.                             }
  919.                         fprintf(cout,"    printf(\"</SELECT>\");\n");
  920.                         fprintf(cout,"    if ((ag_data==AG_ERROR)&&(ag_errors[%d].error==1)) printf(\"<B> Error: %%s</B>\",ag_errors[%d].message);\n",a,a);
  921.                         fprintf(cout,"    printf(\"\\n\");\n");
  922.                         }
  923.                     else {
  924.                         if (strcmp(form[d].field[a].type,"SEQUENCE")==0) {
  925.                             fprintf(cout,"    printf(\"<TD><INPUT TYPE=\\\"HIDDEN\\\" NAME=\\\"%s_%d\\\" SIZE=%s MAXLENGTH=%s\");\n",caps(form[d].field[a].name),b,form[d].field[a].length,form[d].field[a].length);
  926.                             if (choices[d][a].no>0) fprintf(cout,"    printf(\" VALUE=\\\"%d\\\"><CODE>%s</CODE\");\n",b+1,choices[d][a].val[b]);
  927.                             else fprintf(cout,"    printf(\" VALUE=\\\"%d\\\"><CODE>%d</CODE\");\n",b+1,b+1);
  928.                             }
  929.                         else fprintf(cout,"    printf(\"<TD><INPUT TYPE=\\\"%s\\\" NAME=\\\"%s_%d\\\" SIZE=%s MAXLENGTH=%s\");\n",form[d].field[a].type,caps(form[d].field[a].name),b,form[d].field[a].length,form[d].field[a].length);
  930.                         fprintf(cout,"    if (ag_data==AG_FULL) printf(\" VALUE=\\\"%%s\\\"\",PQgetvalue(ag_res,%d,%d));\n",b,fc+a);
  931.                         fprintf(cout,"    if (ag_data>AG_FULL) printf(\" VALUE=\\\"%%s\\\"\",ag_parse(ag_buff,\"%s_%d\",1));\n",caps(form[d].field[a].name),b);
  932.                         fprintf(cout,"    printf(\">\");\n");
  933.                         fprintf(cout,"    if ((ag_data==AG_ERROR)&&(ag_errors[%d].error==1)) {\n",a);
  934.                         fprintf(cout,"        printf(\"<B> Error: %%s</B>\",ag_errors[%d].message);\n",a);
  935.                         fprintf(cout,"        ag_errors[%d].error=0;\n",a);
  936.                         fprintf(cout,"        }\n");
  937.                         fprintf(cout,"    printf(\"\\n\");\n");
  938.                         }
  939.                     }
  940.                 }
  941.             }
  942.         else {    
  943.             for (a=0; a<form[d].fields; a++) {
  944.                 if (strcmp(form[d].field[a].type,"CHOICE")==0) {
  945.                     fprintf(cout,"    printf(\"<TR><TD>%s<TD><SELECT NAME=\\\"%s\\\">\\n\");\n",form[d].field[a].description,caps(form[d].field[a].name));
  946.                     for (c=0; c<choices[d][a].no; c++) {
  947.                         fprintf(cout,"    printf(\"<OPTION VALUE=\\\"%d\\\"\");\n",c);
  948.                         fprintf(cout,"    if (ag_data==AG_FULL) if (atoi(PQgetvalue(ag_res,0,%d))==%d) printf(\" SELECTED\");\n",a,c);
  949.                         fprintf(cout,"    if (ag_data>AG_FULL) if (atoi(ag_parse(ag_buff,\"%s\",1))==%d) printf(\" SELECTED\");\n",caps(form[d].field[a].name),c);
  950.                         fprintf(cout,"    printf(\">%s\");\n",choices[d][a].val[c]);
  951.                         }
  952.                     fprintf(cout,"    printf(\"</SELECT>\");\n");
  953.                     fprintf(cout,"    if ((ag_data==AG_ERROR)&&(ag_errors[%d].error==1)) printf(\"<B> Error: %%s</B>\",ag_errors[%d].message);\n",a,a);
  954.                     fprintf(cout,"    printf(\"\\n\");\n");
  955.                     fc++;
  956.                     }
  957.                 else {
  958.                     fprintf(cout,"    printf(\"<TR><TD>%s<TD><INPUT TYPE=\\\"%s\\\" NAME=\\\"%s\\\" SIZE=%s MAXLENGTH=%s\");\n",form[d].field[a].description,form[d].field[a].type,caps(form[d].field[a].name),form[d].field[a].length,form[d].field[a].length);
  959.                     fprintf(cout,"    if (ag_data==AG_FULL) printf(\" VALUE=\\\"%%s\\\"\",PQgetvalue(ag_res,0,%d));\n",a);
  960.                     fprintf(cout,"    if (ag_data>AG_FULL) printf(\" VALUE=\\\"%%s\\\"\",ag_parse(ag_buff,\"%s\",1));\n",caps(form[d].field[a].name));
  961.                     fprintf(cout,"    printf(\">\");\n");
  962.                     if (strcmp(form[d].field[a].type,"PICK")==0) {
  963.                         fprintf(cout,"    printf(\"<INPUT TYPE=submit NAME=\\\"AG_PROCESS\\\" VALUE=\\\"%s\\\">\");\n",caps(choices[d][a].val[0]));
  964.                         }
  965.                     fprintf(cout,"    if ((ag_data==AG_ERROR)&&(ag_errors[%d].error==1)) {\n",a);
  966.                     fprintf(cout,"        printf(\"<B> Error: %%s</B>\",ag_errors[%d].message);\n",a);
  967.                     fprintf(cout,"        ag_errors[%d].error=0;\n",a);
  968.                     fprintf(cout,"        }\n");
  969.                     fprintf(cout,"    printf(\"\\n\");\n");
  970.                     fc++;
  971.                     }
  972.                 }
  973.             }
  974.         fprintf(cout,"    printf(\"</TABLE>\\n\");\n");
  975.         }
  976.         
  977.     fprintf(cout,"    printf(\"<HR>\\n\");\n");
  978.     fprintf(cout,"    printf(\"<TABLE><TR>\\n\");\n");
  979.     for (a=0; a<no_tools; a++) {
  980.         if (toolbar[a].form==f) {
  981.             if (toolbar[a].a_d==1) fprintf(cout,"    if (ag_data) ");
  982.             else fprintf(cout,"    ");
  983.             fprintf(cout,"printf(\"<TD><INPUT TYPE=submit NAME=\\\"AG_PROCESS\\\" VALUE=\\\"%s\\\">\");\n",toolbar[a].name);
  984.             }
  985.         }
  986.     fprintf(cout,"    printf(\"</TABLE><HR></FORM><A HREF=\\\"%s/%s?AG_PROCESS=AG_ABOUT\\\"><IMG SRC=\\\"/ag/ag_grey.gif\\\" BORDER=0></A></BODY></HTML>\");\n",cgi_url,section.module[m].name);
  987.     fprintf(cout,"    if (ag_data==AG_FULL) PQclear(ag_res);\n");
  988.     fprintf(cout,"    exit(0);\n");
  989.     fprintf(cout,"}\n\n");    
  990. }
  991.  
  992. void findtools(FILE *input,int m)
  993. {
  994.     int a,b=0;
  995.     char line[LINE_SIZE],word[WORD_SIZE],worda[WORD_SIZE];
  996.     for (a=0; a<section.module[m].no_forms; a++) {
  997.         gotoline(input,section.module[m].form[a].line+1);
  998.         freadln(input,line);
  999.         while ((strcmp(getword(1,line,word),"DEFINE")!=0) || (strcmp(getword(2,line,worda),"TOOLBAR")!=0)) freadln(input,line);
  1000.         freadln(input,line);
  1001.         while(line[0]!='}') {
  1002.             strcpy(toolbar[b].name,stripquotes(getword(1,line,word)));
  1003.             if (strcmp(getword(3,line,word),"ALL")==0) toolbar[b].a_d=0;
  1004.             if (strcmp(getword(3,line,word),"DATA")==0) toolbar[b].a_d=1;
  1005.             if (strcmp(getword(5,line,word),"PROCESS")==0) toolbar[b].p_m=1;
  1006.             if (strcmp(getword(5,line,word),"MENU")==0) toolbar[b].p_m=2;
  1007.             if (strcmp(getword(5,line,word),"MODULE")==0) {
  1008.                 toolbar[b].p_m=3;
  1009.                 if (countword(line)>6) strcpy(toolbar[b].process,stripquotes(getword(8,line,word)));
  1010.                 if (countword(line)>8) {
  1011.                     strcpy(worda,stripquotes(getword(10,line,word)));
  1012.                     strcpy(toolbar[b].value,caps(worda));
  1013.                     }
  1014.                 else strcpy(toolbar[b].process,"");
  1015.                 strcpy(toolbar[b].module,stripquotes(getword(6,line,word)));
  1016.                 }
  1017.             else strcpy(toolbar[b].process,stripquotes(getword(6,line,word)));
  1018.             toolbar[b].form=a;
  1019.             b++;
  1020.             freadln(input,line);
  1021.             }
  1022.         }
  1023.     no_tools=b;
  1024. }
  1025.  
  1026.  
  1027.  
  1028. void def_var(char *line, FILE *out, int m, int p)
  1029. {
  1030.     char type[WORD_SIZE],word[WORD_SIZE];
  1031.     getword(1,line,type);
  1032.     if (strcmp(type,"DATA")==0) {
  1033.         fprintf(out,"    PGresult* %s;\n",getword(2,line,word));
  1034.         strcpy(variable[no_vars].name,getword(2,line,word));
  1035.         variable[no_vars].type=DATA;
  1036.         if (p=999) variable[no_vars].g_l=GLOBAL;
  1037.         else {
  1038.             variable[no_vars].g_l=LOCAL;
  1039.             variable[no_vars].process=p;
  1040.             }
  1041.         variable[no_vars].module=m;
  1042.         no_vars++;
  1043.         return;
  1044.         }
  1045.     if (strcmp(type,"INT")==0) {
  1046.         fprintf(out,"    int %s;\n",getword(2,line,word));
  1047.         strcpy(variable[no_vars].name,getword(2,line,word));
  1048.         variable[no_vars].type=INT;
  1049.         if (p=999) variable[no_vars].g_l=GLOBAL;
  1050.         else {
  1051.             variable[no_vars].g_l=LOCAL;
  1052.             variable[no_vars].process=p;
  1053.             }
  1054.         variable[no_vars].module=m;
  1055.         no_vars++;
  1056.         return;
  1057.         }
  1058.     if (strcmp(type,"STRING")==0) {
  1059.         fprintf(out,"    char %s[%d];\n",getword(2,line,word),WORD_SIZE);
  1060.         strcpy(variable[no_vars].name,getword(2,line,word));
  1061.         variable[no_vars].type=STRING;
  1062.         if (p=999) variable[no_vars].g_l=GLOBAL;
  1063.         else {
  1064.             variable[no_vars].g_l=LOCAL;
  1065.             variable[no_vars].process=p;
  1066.             }
  1067.         variable[no_vars].module=m;
  1068.         no_vars++;
  1069.         return;
  1070.         }
  1071.     fprintf(stderr,"ERROR: Unknown variable type: %s\n", type);
  1072.     exit(1);
  1073.  
  1074. }
  1075.  
  1076. void menubuild(int m, char *inputfile)
  1077. {
  1078.     FILE *input,*htmlout;
  1079.     char line[LINE_SIZE],word[WORD_SIZE],title[WORD_SIZE];
  1080.     char filename[MAX_PATH];
  1081.     char set_section[WORD_SIZE];
  1082.     char type[WORD_SIZE];
  1083.     int a,b;
  1084.     printf("    Creating menu - %s\n",section.menu[m].name);
  1085.     strcpy(filename,src_dir);
  1086.     strcat(filename,"/");
  1087.     strcat(filename,section.menu[m].name);
  1088.     strcat(filename,".html");
  1089.     strcpy(set_section,"MENU ");
  1090.     strcat(set_section,section.menu[m].name);
  1091.     input=fopen(inputfile,"rt");
  1092.     if (input==NULL) {
  1093.         fprintf(stderr,"ERROR: Cannot open temporary file %s\n",inputfile);
  1094.         exit(1);
  1095.         }
  1096.     htmlout=fopen(filename,"wt");
  1097.     if (htmlout==NULL) {
  1098.         fprintf(stderr,"ERROR: Cannot create menu file %s\n",filename);
  1099.         exit(1);
  1100.         }
  1101.     fprintf(htmlout,"<HTML>\n");
  1102.     gotoline(input,section.menu[m].line+1);
  1103.     freadln(input,line);
  1104.     while ((line[0]!='[') && (!feof(input))) {
  1105.         if (strcmp(getword(1,line,word),"SET")==0) {
  1106.             check_set(line,set_section);
  1107.             if (strcmp(getword(2,line,word),"title")==0) {
  1108.                 fprintf(htmlout,"<HEAD><TITLE>%s</TITLE></HEAD><BODY BACKGROUND=\"/ag/ag_texture.gif\">\n",stripquotes(getword(4,line,word)));
  1109.                 fprintf(htmlout,"<H1 ALIGN=center>%s</H1><HR>\n",stripquotes(getword(4,line,word)));
  1110.                 }
  1111.             }
  1112.         if (strcmp(getword(1,line,word),"SHOWLN")==0) fprintf(htmlout,"%s<BR>\n",stripquotes(getword(2,line,word)));
  1113.         if (strcmp(getword(1,line,word),"SHOW")==0) fprintf(htmlout,"%s",stripquotes(getword(2,line,word)));
  1114.         if ((strcmp(getword(1,line,word),"DEFINE")==0)&&(strcmp(getword(2,line,word),"MENU")==0)) {
  1115.             fprintf(htmlout,"<H2 ALIGN=center>\n");
  1116.             freadln(input,line);
  1117.             while((line[0]!='}') && (!feof(input)) && (line[0]!='[')) {
  1118.                 if (strcmp(getword(3,line,word),"MENU")==0) fprintf(htmlout,"<A HREF=\"%s/%s.html\">",doc_url,stripquotes(getword(4,line,word)));
  1119.                 if (strcmp(getword(3,line,word),"MODULE")==0) fprintf(htmlout,"<A HREF=\"%s/%s\">",cgi_url,stripquotes(getword(4,line,word)));
  1120.                 fprintf(htmlout,"%s</A><BR><BR>\n",stripquotes(getword(1,line,word)));
  1121.                 freadln(input,line);
  1122.                 }
  1123.             fprintf(htmlout,"</H2>\n");
  1124.             }
  1125.         freadln(input,line);
  1126.         }
  1127.     fprintf(htmlout,"</BODY></HTML>\n");
  1128.     fclose(input);
  1129.     fclose(htmlout);
  1130. }
  1131.  
  1132. void parseglobal(char *filename)
  1133. {
  1134.     FILE *input;
  1135.     char line[LINE_SIZE],word[WORD_SIZE],temp[WORD_SIZE];
  1136.     int a,b,done=0;
  1137.     input=fopen(filename,"rt");
  1138.     if (input==NULL) {
  1139.         fprintf(stderr,"ERROR: Cannot open temporary file %s\n",filename);
  1140.         exit(1);
  1141.         }
  1142.     gotoline(input,section.global_line+1);
  1143.     freadln(input,line);
  1144.     do {
  1145.         if (strcmp(getword(1,line,word),"SET")!=0) {
  1146.             fprintf(stderr,"ERROR: Unrecognised command %s in GLOBAL section.\n",word);
  1147.             exit(1);
  1148.             }
  1149.         check_set(line,"GLOBAL");
  1150.         if (strcmp(getword(2,line,word),"pgdir")==0) strcpy(pgdir,stripquotes(getword(4,line,word)));
  1151.         if (strcmp(getword(2,line,word),"pghost")==0) strcpy(pghost,stripquotes(getword(4,line,word)));
  1152.         if (strcmp(getword(2,line,word),"pgdatabase")==0) strcpy(pgdatabase,stripquotes(getword(4,line,word)));
  1153.         if (strcmp(getword(2,line,word),"httpd-dir")==0) strcpy(httpd_dir,stripquotes(getword(4,line,word)));
  1154.         if (strcmp(getword(2,line,word),"cgi-url")==0) {
  1155.             strcpy(cgi_url,stripquotes(getword(4,line,word)));
  1156.             b=0;
  1157.             for (a=0; ((a<strlen(cgi_url))&&(b<3)); a++) {
  1158.                 if (cgi_url[a]=='/') b++;
  1159.                 }
  1160.             strcpy(cgi_dir,httpd_dir);
  1161.             for (b=a; b<strlen(cgi_url); b++) temp[b-a]=cgi_url[b];
  1162.             temp[b-a]=0;
  1163.             strcat(cgi_dir,"/");
  1164.             strcat(cgi_dir,temp);
  1165.             }
  1166.         if (strcmp(getword(2,line,word),"doc-url")==0) {
  1167.             strcpy(doc_url,stripquotes(getword(4,line,word)));
  1168.             b=0;
  1169.             for (a=0; ((a<strlen(doc_url))&&(b<3)); a++) {
  1170.                 if (doc_url[a]=='/') b++;
  1171.                 }
  1172.             strcpy(doc_dir,httpd_dir);
  1173.             for (b=a; b<strlen(doc_url); b++) temp[b-a]=doc_url[b];
  1174.             temp[b-a]=0;
  1175.             strcat(doc_dir,"/");
  1176.             strcat(doc_dir,temp);
  1177.             }
  1178.         if (strcmp(getword(2,line,word),"src-dir")==0) strcpy(src_dir,stripquotes(getword(4,line,word)));
  1179.         freadln(input,line);
  1180.         } while ((line[0]!='[') && (!feof(input)));    
  1181.     if (strlen(pgdir)==0) {
  1182.         printf("WARNING: pgdir not set in GLOBAL section, assuming \"/usr/local/postgres95\"\n");
  1183.         strcpy(pgdir,"/usr/local/postgres95");
  1184.         }
  1185.     if (strlen(pghost)==0) {
  1186.         printf("WARNING: pghost not set in GLOBAL section, assuming \"localhost\"\n");
  1187.         strcpy(pghost,"localhost");
  1188.         }
  1189.     if (strlen(pgdatabase)==0) {
  1190.         printf("WARNING: pgdatabase not set in GLOBAL section, assuming \"root\"\n");
  1191.         strcpy(pgdatabase,"root");
  1192.         }
  1193.     if (strlen(httpd_dir)==0) {
  1194.         printf("WARNING: httpd-dir not set in GLOBAL section, assuming \"/usr/local/httpd\"\n");
  1195.         strcpy(httpd_dir,"/usr/local/httpd");
  1196.         }
  1197.     if (strlen(cgi_url)==0) {
  1198.         printf("WARNING: cgi_url not set in GLOBAL section, assuming \"http://localhost/cgi-bin\"\n");
  1199.         strcpy(cgi_url,"http://localhost/cgi-bin");
  1200.         }
  1201.     if (strlen(doc_url)==0) {
  1202.         printf("WARNING: doc_url not set in GLOBAL section, assuming \"http://localhost/docs\"\n");
  1203.         strcpy(doc_url,"http://localhost/docs");
  1204.         }
  1205.     if (strlen(src_dir)==0) {
  1206.         printf("WARNING: src_dir not set in GLOBAL section, assuming \".\"\n");
  1207.         strcpy(src_dir,".");
  1208.         }
  1209. }
  1210.  
  1211. void check_set(char *line, char *section)
  1212. {
  1213.     char variable[WORD_SIZE], as[WORD_SIZE], value[WORD_SIZE],spare[WORD_SIZE];
  1214.     getword(2,line,variable);
  1215.     getword(3,line,as);
  1216.     getword(4,line,value);
  1217.     if (strcmp(section,"GLOBAL")==0) {
  1218.         if (checkin(variable,"pgdir*pghost*pgdatabase*httpd-dir*cgi-url*doc-url*src-dir")!=1) {
  1219.             fprintf(stderr,"> %s\nERROR: Invalid entity \"%s\" to SET in GLOBAL section\n",line,variable);
  1220.             exit(1);
  1221.             }
  1222.         }
  1223.     if (section[1]=='E') {
  1224.         if (strcmp(variable,"title")!=0) {
  1225.             fprintf(stderr,"> %s\nERROR: Invalid entity \"%s\" to SET in section %s\n",line,variable,section);
  1226.             exit(1);
  1227.             }
  1228.         }
  1229.     if (strcmp(as,"AS")!=0) {
  1230.             fprintf(stderr,"> %s\nERROR: Expecting AS in SET in section %s\n",line,section);
  1231.             exit(1);
  1232.             }
  1233.     if ((value[0]!='\"')||(value[strlen(value)-1]!='\"')) {
  1234.             fprintf(stderr,"> %s\nERROR: Value should be in quotes in SET in section %s\n",line,section);
  1235.             exit(1);
  1236.             }
  1237. }
  1238.  
  1239. int checkin(char *value, char *range)
  1240. {
  1241.     int found=0,a,b;
  1242.     for (b=0; ((b<strlen(range))&&(!found)); b++) {
  1243.         found=1;
  1244.         for (a=0; ((a<strlen(value))&&(a+b<strlen(range))); a++) if (value[a]!=range[b+a]) found=0;
  1245.          }    
  1246.     return(found);
  1247. }
  1248.  
  1249. char *stripquotes(char *word) 
  1250. {
  1251.     char output[WORD_SIZE];
  1252.     int a,b;
  1253.     b=0;
  1254.     a=0; 
  1255.     while ((word[a]!='\"') && (a<strlen(word))) a++;
  1256.     a++;
  1257.     while ((word[a]!='\"') && (a<strlen(word))) {
  1258.         output[b]=word[a];
  1259.         a++;
  1260.         b++;
  1261.         }
  1262.     output[b]=0;
  1263.     strcpy(word,output);
  1264.     return(word);
  1265. }
  1266.  
  1267. void modulepass(char *filename, int m)
  1268. {
  1269.     FILE *input;
  1270.     char line[LINE_SIZE],word[WORD_SIZE];
  1271.     int a,b;
  1272.     input=fopen(filename,"rt");
  1273.     if (input==NULL) {
  1274.         fprintf(stderr,"ERROR: Cannot open temporary file %s\n",filename);
  1275.         exit(1);
  1276.         }
  1277.     section.module[m].no_forms=0;
  1278.     section.module[m].no_process=0;
  1279.     section.module[m].no_outputs=0;
  1280.     a=section.module[m].line+1; 
  1281.     gotoline(input,a);
  1282.     while (!feof(input)) {    
  1283.         freadln(input,line);
  1284.         if (strcmp(getword(1,line,word),"[FORM")==0) {
  1285.             section.module[m].form[section.module[m].no_forms].line=a;
  1286.             strcpy(section.module[m].form[section.module[m].no_forms].name,getword(2,line,word));
  1287.             for (b=0; b<strlen(section.module[m].form[section.module[m].no_forms].name); b++) if (section.module[m].form[section.module[m].no_forms].name[b]==']') section.module[m].form[section.module[m].no_forms].name[b]=0;
  1288.             section.module[m].no_forms++;
  1289.             }
  1290.         if (strcmp(getword(1,line,word),"[PROCESS")==0) {
  1291.             section.module[m].process[section.module[m].no_process].line=a;
  1292.             strcpy(section.module[m].process[section.module[m].no_process].name,getword(2,line,word));
  1293.             for (b=0; b<strlen(section.module[m].process[section.module[m].no_process].name); b++) if (section.module[m].process[section.module[m].no_process].name[b]==']') section.module[m].process[section.module[m].no_process].name[b]=0;
  1294.             section.module[m].no_process++;
  1295.             }
  1296.         if (strcmp(getword(1,line,word),"[OUTPUT")==0) {
  1297.             section.module[m].output[section.module[m].no_outputs].line=a;
  1298.             strcpy(section.module[m].output[section.module[m].no_outputs].name,getword(2,line,word));
  1299.             for (b=0; b<strlen(section.module[m].output[section.module[m].no_outputs].name); b++) if (section.module[m].output[section.module[m].no_outputs].name[b]==']') section.module[m].output[section.module[m].no_outputs].name[b]=0;
  1300.             section.module[m].no_outputs++;
  1301.             }
  1302.         if (strcmp(getword(1,line,word),"[MODULE")==0) return; 
  1303.         if (strcmp(getword(1,line,word),"[MENU")==0) return; 
  1304.  
  1305.         a++;
  1306.         }
  1307.     fclose(input);
  1308. }
  1309.  
  1310.  
  1311. void firstpass(char *filename)
  1312. {
  1313.     FILE *input;
  1314.     char line[LINE_SIZE],word[WORD_SIZE];
  1315.     int a,b,globals=0;
  1316.     input=fopen(filename,"rt");
  1317.     if (input==NULL) {
  1318.         fprintf(stderr,"ERROR: Cannot open temporary file %s\n",filename);
  1319.         exit(1);
  1320.         }
  1321.     cline=0;    
  1322.     section.no_menus=0;
  1323.     section.no_modules=0;
  1324.     gotoline(input,1);
  1325.     a=0;    
  1326.     while (!feof(input)) {
  1327.         freadln(input,line);
  1328.         a++;
  1329.         if (strcmp(getword(1,line,word),"[GLOBAL]")==0) {
  1330.             section.global_line=a;
  1331.             globals++;
  1332.             }
  1333.         if (strcmp(getword(1,line,word),"[MENU")==0) {
  1334.             section.menu[section.no_menus].line=a;
  1335.             strcpy(section.menu[section.no_menus].name,getword(2,line,word));
  1336.             for (b=0; b<strlen(section.menu[section.no_menus].name); b++) if (section.menu[section.no_menus].name[b]==']') section.menu[section.no_menus].name[b]=0;
  1337.             section.no_menus++;
  1338.             }
  1339.         if (strcmp(getword(1,line,word),"[MODULE")==0) {
  1340.             section.module[section.no_modules].line=a;
  1341.             strcpy(section.module[section.no_modules].name,getword(2,line,word));
  1342.             for (b=0; b<strlen(section.module[section.no_modules].name); b++) if (section.module[section.no_modules].name[b]==']') section.module[section.no_modules].name[b]=0;
  1343.             section.no_modules++;    
  1344.             }
  1345.         }
  1346.     if (globals==0) {
  1347.         fprintf(stderr,"ERROR: No GLOBAL section found in input file.\n");
  1348.         exit(1);
  1349.         }
  1350.     if (globals>1) {
  1351.         printf("WARNING: More than one GLOBAL section found, using latest.\n");
  1352.         }
  1353.     if ((section.no_menus==0) && (section.no_modules==0)) {
  1354.         fprintf(stderr,"ERROR: No menus or modules found in input file.\n");
  1355.         exit(1);
  1356.         }
  1357.     fclose(input);
  1358. }
  1359.  
  1360. char *getword(int word_number, char *line, char *word)
  1361. {
  1362.     int a,b,c,in_quotes=0;
  1363.     b=0;
  1364.     for (a=0; a<WORD_SIZE; a++) word[a]=0;
  1365.     a=0;
  1366.     c=0;
  1367.     while(a<strlen(line)) {
  1368.         if (!in_quotes) if ((line[a]==' ') || (line[a]=='\t')) {
  1369.             while (((line[a]==' ')||(line[a]=='\t'))&&(a<strlen(line))) a++;
  1370.             word[b]=0;
  1371.             b=0;
  1372.             c++;
  1373.             if (c==word_number) return(word);
  1374.             }
  1375.         if (in_quotes) if (line[a]=='\"') {
  1376.             a++;
  1377.             while (((line[a]==' ')||(line[a]=='\t'))&&(a<strlen(line))) a++;
  1378.             word[b]='\"';
  1379.             b++;
  1380.             word[b]=0;
  1381.             b=0;
  1382.             c++;
  1383.             in_quotes=0;
  1384.             if (c==word_number) return(word);
  1385.             }
  1386.         if (line[a]=='\"') {
  1387.             if (in_quotes==1) in_quotes=0;
  1388.             else in_quotes=1;
  1389.             }
  1390.         word[b]=line[a];
  1391.         b++;
  1392.         a++;
  1393.         }
  1394.     word[b]=0;
  1395.     return(word);
  1396. }
  1397.  
  1398. int countword(char *line)
  1399. {
  1400.     int a,b,c,in_quotes=0;
  1401.     char word[WORD_SIZE];
  1402.     int word_number=99;
  1403.     b=0;
  1404.     for (a=0; a<WORD_SIZE; a++) word[a]=0;
  1405.     a=0;
  1406.     c=0;
  1407.     while(a<strlen(line)) {
  1408.         if (!in_quotes) if ((line[a]==' ') || (line[a]=='\t')) {
  1409.             while (((line[a]==' ')||(line[a]=='\t'))&&(a<strlen(line))) a++;
  1410.             word[b]=0;
  1411.             b=0;
  1412.             c++;
  1413.             }
  1414.         if (in_quotes) if (line[a]=='\"') {
  1415.             a++;
  1416.             while (((line[a]==' ')||(line[a]=='\t'))&&(a<strlen(line))) a++;
  1417.             word[b]='\"';
  1418.             b++;
  1419.             word[b]=0;
  1420.             b=0;
  1421.             c++;
  1422.             in_quotes=0;
  1423.             }
  1424.         if (line[a]=='\"') {
  1425.             if (in_quotes==1) in_quotes=0;
  1426.             else in_quotes=1;
  1427.             }
  1428.         word[b]=line[a];
  1429.         b++;
  1430.         a++;
  1431.         }
  1432.     word[b]=0;
  1433.     return(c+1);
  1434. }
  1435.  
  1436.  
  1437. void gotoline(FILE *input, int line_number)
  1438. {
  1439.     int a,d;
  1440.     char spare[LINE_SIZE];
  1441.     d = (line_number - 1);
  1442.     fseek(input,0,SEEK_SET);
  1443.     for (a=0; a<d; a++) freadln(input,spare);
  1444. }    
  1445.  
  1446. int create_tmp(FILE *tmp,char *input_file,int pass) 
  1447. {
  1448.     FILE *input;
  1449.     char line[LINE_SIZE],filename[MAX_PATH],word[WORD_SIZE];
  1450.     int a,b,no_lines=0;
  1451.     input=fopen(input_file,"rt");
  1452.     if (input==NULL) {
  1453.         fprintf(stderr,"ERROR: Cannot open INCLUDE file %s\n",input_file);
  1454.         exit(1);
  1455.         }
  1456.     while (!feof(input)) {
  1457.         freadln(input,line);
  1458.         if ((line[0]=='I') && (line[1]=='N') && (line[2]=='C') && (line[3]=='L') && (line[4]=='U') && (line[5]=='D') && (line[6]=='E')) {
  1459.             a=0; 
  1460.             while ((a<strlen(line))&&(line[a]!='\"')) a++;
  1461.             a++;
  1462.             b=0;
  1463.             while ((a<strlen(line))&&(line[a]!='\"')) {
  1464.                 filename[b]=line[a];
  1465.                 a++;
  1466.                 b++;
  1467.                 }
  1468.              filename[b]=0;
  1469.             create_tmp(tmp,filename,pass);
  1470.             }
  1471.         else if (strlen(line)>0) {
  1472.             for (b=0; b<strlen(line); b++) {
  1473.                 if ((pass==1)&&(line[b]=='B')&&(line[b+1]=='E')&&(line[b+2]=='G')&&(line[b+3]=='I')&&(line[b+4]=='N')) fprintf(tmp,"\n");
  1474.                 fprintf(tmp,"%c",line[b]);
  1475.                 if ((pass==1)&&(line[b+2]!='B')&&(line[b]=='N')&&(line[b-1]=='E')&&(line[b-2]=='H')&&(line[b-3]=='T')&&(b>4)) fprintf(tmp,"\n"); 
  1476.                 }
  1477.             fprintf(tmp,"\n");
  1478.             no_lines++;
  1479.             }
  1480.         }
  1481.     fclose(input);
  1482.     return(no_lines);
  1483. }
  1484.  
  1485. int freadln(FILE *input,char *output)
  1486. {
  1487.     int a,c,size=0;
  1488.     cline++;
  1489.     for (a=0; a<LINE_SIZE; a++) output[a]=0;
  1490.     c=fgetc(input);
  1491.     if (feof(input)) return(0);
  1492.     if (c=='#') {
  1493.         while(fgetc(input)!='\n');
  1494.         return(0);
  1495.         }
  1496.     if (c=='\n') return(0);
  1497.     while ((c==' ') || (c=='\t')) c=fgetc(input);    
  1498.     output[0]=c;
  1499.     a=1;
  1500.     do {
  1501.         c=fgetc(input);
  1502.         output[a]=c;
  1503.         a++;
  1504.         } while((a<LINE_SIZE) && (c!='\n') && (!feof(input))); 
  1505.     a--;
  1506.     output[a]=0;
  1507.     return(a);
  1508. }
  1509.  
  1510.  
  1511. char *caps(char* var1)
  1512. {
  1513.     int a;
  1514.     char var[WORD_SIZE];
  1515.     strcpy(var, var1);
  1516.     for (a=0; a<strlen(var); a++) if (var[a]>=97) var[a]-=97-65;
  1517.     return(var);
  1518. }
  1519.         
  1520.